package com.ruoyi.medicine.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.medicine.domain.Cart;
import com.ruoyi.medicine.domain.HisMedicineDrug;
import com.ruoyi.medicine.mapper.HisMedicineCartMapper;
import com.ruoyi.medicine.mapper.HisMedicineDrugMapper;
import com.ruoyi.medicine.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    //声明mapper层属性
    @Autowired
    private HisMedicineCartMapper mapper;
    @Autowired
    private HisMedicineDrugMapper drugMapper;
    //添加药品到购物车
    @Override
    @Transactional
    public int addCart(Long userId, Long[] drugIds) throws JsonProcessingException{
        //创建ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();
        //1.查询用户的购物车
        Cart cart = mapper.selectHisMedicineCartByUserId(userId);
        //2. 判断购物车是否存在,存在则获取当前的购物车，不存在则创建新的购物车
        HashMap<Long, HisMedicineDrug> map=null;
        if(cart==null){
            map=new HashMap<>();
        }else{
            //获取原来数据库当中的药品
            map=objectMapper.readValue(cart.getCart(), new TypeReference<HashMap<Long,HisMedicineDrug>>() {});
        }
        //3. 添加药品信息到购物车中
        for(Long drugId: drugIds){
            //判断购物车中是否已经有对应的药品
            HisMedicineDrug hisMedicineDrug = map.get(drugId);
            if(hisMedicineDrug==null){
                HisMedicineDrug medicineDrug = drugMapper.selectHisMedicineDrugByDrugId(drugId);
                medicineDrug.setCartDrugNum(1L);
                //在map中追加需要添加的新药品
                map.put(drugId,medicineDrug);
            }
        }

        //4. 存储购物车
        String s = objectMapper.writeValueAsString(map);
        if(cart==null){
            //新增购物车
            cart=new Cart();
            cart.setCart(s);
            cart.setUserId(userId);
            return mapper.insertCartInfoMapper(cart);
        }else{
            //更新购物车
            cart.setCart(s);
            return mapper.updateCartInfoMapper(cart);
        }
    }

    //我的采购
    @Override
    public List<HisMedicineDrug> myCartListService(Long userId) throws JsonProcessingException {
        //创建ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();
        //1.查询用户的购物车
        Cart cart = mapper.selectHisMedicineCartByUserId(userId);
        //2. 判断购物车是否存在,存在则获取当前的购物车，不存在则创建新的购物车
        if(cart!=null){
           HashMap<Long,HisMedicineDrug> map=objectMapper.readValue(cart.getCart(), new TypeReference<HashMap<Long,HisMedicineDrug>>() {});
            Collection<HisMedicineDrug> values = map.values();
            List<HisMedicineDrug> list=new ArrayList<>(values);
            return list;
        }
        return null;
    }
    //更新采购数量
    @Override
    public int updateCartNumService(Long userId, Long drugId, Long cartDrugNum) throws JsonProcessingException {
        //创建ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();
        //1.查询用户的购物车
        Cart cart = mapper.selectHisMedicineCartByUserId(userId);
        //获取用户当前的购物车当中的数据
        HashMap<Long,HisMedicineDrug> map=objectMapper.readValue(cart.getCart(), new TypeReference<HashMap<Long,HisMedicineDrug>>() {});
        //获取需要修改的那一条数据
        HisMedicineDrug hisMedicineDrug = map.get(drugId);
        //修改数据数量
        System.out.println(cartDrugNum);
        hisMedicineDrug.setCartDrugNum(cartDrugNum);
        //转成JSON格式
        String s = objectMapper.writeValueAsString(map);
        //重新赋值给购物车对象
        cart.setCart(s);
        System.out.println(cart);
        //调用方法
        return mapper.updateCartInfoMapper(cart);
    }

    @Override
    public int deleteCartByDrugIds(Long userId,Long[] drugIds) throws JsonProcessingException {
        //创建ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();
        //1.查询用户的购物车
        Cart cart = mapper.selectHisMedicineCartByUserId(userId);
        //获取用户当前的购物车当中的数据
        HashMap<Long,HisMedicineDrug> map=objectMapper.readValue(cart.getCart(), new TypeReference<HashMap<Long,HisMedicineDrug>>() {});
        //删除集合中和数组相等的键所对应的值
        for (Long drugId:drugIds){
            map.remove(drugId);
        }
        //转成JSON格式
        String s = objectMapper.writeValueAsString(map);
        //重新赋值给购物车对象
        cart.setCart(s);
        return  mapper.updateCartInfoMapper(cart);
    }
}
